package cn.bingo.yunsj.utils;

import cn.bingo.yunsj.mail.model.FileProperties;
import cn.bingo.yunsj.model.Model;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class ModelUtils {

    //模板存放路径
    private String path = FileProperties.getDocPath();

    public List<Model> getFile() {
        return recursive(path);
    }

    //递归获取文件
    private List<Model> recursive(String path) {
        File file = new File(path);
        File[] files = file.listFiles();
        List<Model> list = new ArrayList<>();

        for (int i = 0; i < files.length; i++) {
            //判断是不是文件夹
            if (files[i].isDirectory()) {
                recursive(files[i].getAbsolutePath());
            } else {
                Model model = new Model();
                String s = files[i].getName();
                model.setFileName(s.substring(0, s.indexOf(".")));
                model.setFileType(s.substring(s.indexOf(".") + 1, s.length()));
                model.setFilePath(files[i].getAbsolutePath());
                model.setDate(new Timestamp(files[i].lastModified()));
                model.setFileSize(files[i].length());
                list.add(model);
            }
        }
        return list;
    }
}
